import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'portfolioTrackingM',

  state: {
    tableData: {},
    rowData: {},
  },

  effects: {
    *getProductData({ payload, callback }, { call, put }) {
      const res = yield call(api.portfolioTracking.getProductData, payload);

      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            tableData: res.data,
          },
        });
        callback(res.data);
      } else {
        message.error(res.msg);
      }
    },

    *getColumn({ payload, callback }, { call, put }) {
      const res = {
        columns: [
          { dataIndex: 'portfolioName', title: '产品名称', key: 'portfolioName' },
          { dataIndex: 'assetType', title: '资产类别', key: 'assetType' },
          { dataIndex: 'stockCode', title: '证券代码', key: 'stockCode' },
          { dataIndex: 'stockName', title: '证券简称', key: 'stockName' },
          { dataIndex: 'costPrice', title: '成本价', key: 'costPrice' },
          { dataIndex: 'newPrice', title: '最新价', key: 'newPrice' },
          { dataIndex: 'totalValue', title: '总市值', key: 'totalValue' },
          { dataIndex: 'weight', title: '权重(%)', key: 'weight' },
          { dataIndex: 'totalPnl', title: '总盈亏', key: 'totalPnl' },
        ],
      };
      callback(res);
    },

    //获取表格树形数据
    *getRowInfo({ payload, callback }, { call, put }) {
      const res = yield call(api.portfolioTracking.getRowInfo, payload);

      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            rowData: res.data,
          },
        });
        callback(res.data);
      } else {
        message.error('查询失败');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
